package chamber.dicegame;

/**
 * Created by Gerardo on 1/17/14.
 */
public class Player extends Thread {
    private String thisPleyer;
    private String otherPlayer;

    public Player(String thisPleyer) {
        this.thisPleyer = thisPleyer;
        otherPlayer = thisPleyer.equals(Gammers.JOE) ? Gammers.JANE : Gammers.JOE;
    }

    @Override
    public void run() {
        for (int i = 0; i < 2; ++i) {
            synchronized (Dice.class) {
                System.out.println("Locked by " + thisPleyer);
                while (!Dice.getTurn().equals(thisPleyer)) {
                    try {
                        System.out.println(thisPleyer + " was waiting for " + otherPlayer);
                        Dice.class.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Dice.getTurn() + " throws " + Dice.roll());
                Dice.setTurn(otherPlayer);
                Dice.class.notifyAll();
            }
        }
    }
}
